package cn.com.dhcc.turbo.dao.registry;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.turbo.entity.exch.GapUserConf;

@Repository
public class GapUserConfDao extends AbstractCrudDao<GapUserConf> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);
	
	/**
	 * 获取网闸所有者节点编码
	 * @return
	 */
	public List<GapUserConf> getUserConfList() throws DaoException {
		String msg = "";
		try {
			String sql = "select distinct a.gap_owner,b.node_name  from "
					+ SI_TABLE("turbo_gap_owner_conf ")
					+"a,"
					+SI_TABLE("turbo_node_info ")
					+"b"
					+" where a.gap_owner=b.node_code ";
			return CM.getDao().listModule("获取网闸所有者节点编码成功", sql, GapUserConf.class, new Object[]{});
		} catch (DaoException e) {
			msg = "获取网闸所有者节点编码失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	};
	
	/**
	 * 
	 * @return
	 * @throws DaoException 
	 */
	public int deleteAll() throws DaoException{
		String sql="delete from "+SI_TABLE(" turbo_gap_user_conf")+" t1 ";
		try {
			return CM.getDao().delete("", sql, new Object[]{});
		} catch (DaoException e) {
			String msg="清空表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
				
			
	}
	

	/**
	 * 更新网闸所有者节点编码
	 * @return
	 */
	public List<GapUserConf>  insertOrUpdataUserConfList(List<GapUserConf> dataList,String localNode) throws DaoException{
		String msg="";
		try{
			if (dataList==null || dataList.isEmpty()) {
				return dataList;
			}
			int count=0;//计算dataList默认网闸，如果默认的标示有两个或者两个以上，则进行重置成1个
			boolean flag=true;//是否设置过默认网闸
			for (GapUserConf gapUserConf : dataList) {
				if (gapUserConf.getStatus()!=null && gapUserConf.getStatus().equals("1")) {
					count=count+1;//统计有几个默认的网闸标示
				}
			}
			
			
			//如果有网闸，且没有默认的网闸，就将本节点的网闸设置为默认的网闸节点
			//如果没有网闸，就取第一个为默认的网闸节点
			if (NodeInfoParam.hasGap()) {
				for (GapUserConf gapUserConf : dataList) {
					if (count<1) {
						if (gapUserConf.getGapOwner().equals(localNode)) {
							gapUserConf.setStatus("1");
						}
					}else if(count>1){
						if (flag) {
							gapUserConf.setStatus("1");
							flag=false;
						}
					}
					insertOrUpdate(gapUserConf);
				}
			}else{
		        for (GapUserConf gapUserConf : dataList) {
					if (flag) {
						gapUserConf.setStatus("1");
						flag=false;
					}else{
						gapUserConf.setStatus("0");
					}
					insertOrUpdate(gapUserConf);
				}
			}
		
		/*
			String sql="merge into "
					+SI_TABLE(" turbo_gap_user_conf t1")
					+" using(select ? as id,? as gap_owner,? as mtime,? as status from dual) t2 on (t1.id='1')   "
					+" when matched then update set t1.gap_owner=t2.gap_owner,t1.mtime=t2.mtime   "
					+" when not matched then insert (id,gap_owner,mtime) values (t2.id,t2.gap_owner,t2.mtime)";
			List<Object[]> paramList=new ArrayList<Object[]>();
			if(CollectionUtil.hasElement(dataList)){
				for(GapUserConf vo:dataList){
					Object[] param=new Object [4];
					param[0] =vo.getId();
					param[1]=vo.getGapOwner();
					param[2]=new Date();
					if (vo.getStatus()!=null && !"".equals(vo.getStatus())) {
						param[3]=vo.getStatus();
					}else{
						param[3]="0";
					}
					paramList.add(param);
				}
			}
			int result=CM.getDao().batchUpdate("更新网闸节点编码成功",sql, paramList);
			//初始一个默认值
			sql="select * from "+SI_TABLE(" turbo_gap_user_conf")+" t1 where t1.status='1'";//查询是否有默认的网闸
			List<GapUserConf> list=CM.getDao().listModule("", sql, GapUserConf.class, new Object[]{});
			if (list==null || list.size()!=1) {//如果没有默认网闸，或者默认网闸有两个，初始化成一个
				if (list==null) {
					if (NodeInfoParam.hasGap()) {//如果有网闸
						sql="update "+SI_TABLE(" turbo_gap_user_conf")+" set status='1' where gap_owner=?";
						CM.getDao().update("", sql, new Object[]{localNode});
					}else{
						sql="update "+SI_TABLE(" turbo_gap_user_conf")+" set status='1' where id=?";
						CM.getDao().update("", sql, new Object[]{dataList.get(0).getId()});
					}
				}else{
					for (int i = 0; i < dataList.size(); i++) {
						GapUserConf c=list.get(i);
						if (!c.getId().equals(list.get(0).getId())) {
							c.setStatus("0");
							update(c);
						}
					}
				}
			}*/
			
			return 	dataList;	
		}catch(DaoException e){
			msg="更新网闸节点编码失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
			
		}
	};

	
}
